# -*- mode: python; -*-

# This SConscript describes construction of buildinfo.cpp, which is independent of the
# build variant's target.

import os
import sys

import buildscripts.utils

Import('env windows usev8')

def getSysInfo():
    if windows:
        return "windows " + str( sys.getwindowsversion() )
    else:
        return " ".join( os.uname() )

buildinfo_filename = '$BUILD_DIR/buildinfo.cpp'
env.NoCache(buildinfo_filename)

buildinfo_template = '''
#include <string>
#include <boost/version.hpp>

#include "mongo/util/version.h"

namespace mongo {
    const char * gitVersion() { return "%(git_version)s"; }
    const char * compiledJSEngine() { return "%(js_engine)s"; }
    const char * allocator() { return "%(allocator)s"; }
    const char * loaderFlags() { return "%(loader_flags)s"; }
    const char * compilerFlags() { return "%(compiler_flags)s"; }
    std::string sysInfo() { return "%(sys_info)s BOOST_LIB_VERSION=" BOOST_LIB_VERSION ; }
}  // namespace mongo
'''

def generate_buildinfo(env, target, source, **kw):
    git_version = buildscripts.utils.getGitVersion()
    if len(env["MONGO_MODULES"]):
        git_version += " modules: " + ", ".join(env["MONGO_MODULES"])

    if usev8:
        js_engine = "V8"
    else:
        js_engine = "Unknown"

    contents = str(source[0]) % dict(git_version=git_version,
                                     js_engine=js_engine,
                                     sys_info=getSysInfo(),
                                     allocator=GetOption('allocator'),
                                     loader_flags=env.subst('$LINKFLAGS $LDFLAGS',
                                                            source=source, target=target),
                                     compiler_flags=env.subst('$CXXFLAGS $CCFLAGS $CFLAGS',
                                                              source=source, target=target))
    out = open(str(target[0]), 'wb')
    try:
        out.write(contents)
    finally:
        out.close()

env.Command(buildinfo_filename,
            Value(buildinfo_template),
            Action(generate_buildinfo, "Generating $TARGET"))
env.AlwaysBuild(buildinfo_filename)
env.Install('$BUILD_DIR/mongo', buildinfo_filename)
env.Install('$BUILD_DIR/client_build/mongo', buildinfo_filename)
